package com.woniuxy.huayoung.controller;

import com.woniuxy.huayoung.entity.TravelNotes;
import com.woniuxy.huayoung.result.ResponseResult;

import com.woniuxy.huayoung.service.TravelNotesService;

import org.springframework.web.bind.annotation.*;
import com.woniuxy.huayoung.service.UserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/travelnotes")
public class TravelNotesController {
    @Resource
    private UserService userService;
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @Resource
    private TravelNotesService travelNotesService;

    //游记推荐页面
    //取点击量排名靠前的游记的封面图作为轮播图展示
    @GetMapping("/carousel")
    public ResponseResult getCarousel(){
        //点击量排名前五的游记
//        List<TravelNotes> topClicks = travelNotesService.findByTopClicks(5);
//        List<String> imgSrcs=null;       //取得每一个游记的封面地址，存入集合中
//        for(TravelNotes tn: topClicks){
//            imgSrcs.add(tn.getCoverImage());
//        }
        List<String> list = travelNotesService.findImgs();
        //return new ResponseResult(200,"success",imgSrcs);
        if (list == null){
            return new ResponseResult(500,"fail",list);
        }
        return new ResponseResult(200,"success",list);
    }

    //根据省份导航查找对应省份游记
    @GetMapping("/navigation")
    public ResponseResult getTravelNotesByLocation(String location){
        List<TravelNotes> results = travelNotesService.findByLocation(location);
        if (results == null){
            return new ResponseResult(500,"fail",results);
        }
        return new ResponseResult(200,"success",results);
    }

    //首页展示最热游记
    @GetMapping("/showHottest")
    public ResponseResult showHottest(){
        List<TravelNotes> results = travelNotesService.showHottest();
        if (results == null){
            return new ResponseResult(500,"fail",results);
        }
        return new ResponseResult(200,"success",results);
    };

    //根据搜索词查询游记
    @GetMapping("/search")
    public ResponseResult search(String words){
        List<TravelNotes> results = travelNotesService.findByWords(words);
        if (results == null){
            return new ResponseResult(500,"fail",results);
        }
        return new ResponseResult(200,"success",results);

    };



    //游记详情页
    //根据id查找对应游记的详细信息
    @GetMapping("/details")
    public ResponseResult showDetails(int id){
        System.out.println(id);
        TravelNotes travelNotes = travelNotesService.showDetails(id);
        System.out.println(travelNotes);
        if (travelNotes == null){
            return new ResponseResult(500,"fail",travelNotes);
        }
        return new ResponseResult(200,"success",travelNotes);
    }

    //编辑游记
    @RequestMapping("/edit")
    public ResponseResult editTravelNotes(@RequestBody TravelNotes travelNotes){
        System.out.println(travelNotes);
        int row = travelNotesService.editTravelNotes(travelNotes);
        String result="游记编辑已保存";
        if (row < 1 ){
            result="游记编辑失败";
            return new ResponseResult(500,"fail",result);
        }
        return new ResponseResult(200,"success",result);
    }

    //删除游记
    @GetMapping("/delete")
    public ResponseResult deleteTravelNotes(int id){
        int row = travelNotesService.deleteTravelNotes(id);
        String result="游记删除成功";
        if (row < 1 ){
            result="游记删除失败";
            return new ResponseResult(500,"fail",result);
        }
        return new ResponseResult(200,"success",result);
    }

    //增加点击量
    @RequestMapping("/addClicks")
    public ResponseResult addClicks(int id){
        int row = travelNotesService.addClicks(id);
        String result="获取详情成功";
        if (row < 1 ){
            result="获取详情失败";
            return new ResponseResult(500,"fail",result);
        }
        return new ResponseResult(200,"success",result);
    }


    //写游记页面
    //添加游记
    @PostMapping("/write")
    public ResponseResult writeTravelNotes(@RequestBody TravelNotes travelNotes){
        int row = travelNotesService.insert(travelNotes);
        String result="游记已保存";
        if (row < 1 ){
            result="游记保存失败";
            return new ResponseResult(500,"fail",result);
        }
        return new ResponseResult(200,"success",result);
    }

    @GetMapping("/findOwnNotes")
    public ResponseResult<List<TravelNotes>> findOwnNotes(HttpServletRequest request){
        //得到token
        String token = request.getHeader("authorization");
        int uid = userService.findIdByToken(token);
        List<TravelNotes> travelNotes = travelNotesService.findByUid(uid);
        logger.debug(travelNotes.toString());
        return new ResponseResult<List<TravelNotes>>(200,"查询成功",travelNotes);
    }

    //通过用户id查询账户下的游记
    @GetMapping("/findById")
    public ResponseResult<List<TravelNotes>> findById(@RequestParam("id") Integer uid){

        List<TravelNotes> travelNotes = travelNotesService.findByUid(uid);
        logger.debug(travelNotes.toString());
        return new ResponseResult<List<TravelNotes>>(200,"查询成功",travelNotes);
    }

    @PostMapping("/delById")
    public ResponseResult<String> delById(@RequestBody Map<String, Integer> map){
        logger.debug(map.get("id").toString());
        int i = travelNotesService.delById(map.get("id"));
        if(i != 0){
            return new ResponseResult<String>(200,"删除成功",null);
        }
        return new ResponseResult<String>(500,"删除失败",null);
    }

}
